package com.vma.jifu.backsys.controller;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.vma.jifu.bizModel.model.MessageLogs;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kitabi on 2017/6/20.
 */
public class BaseController  extends Controller {

    public Map<String,Object> ReturnForm(Object rows,int total){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("total",total);
        result.put("rows",rows);

        return result;
    }

    public static Map<String,Object> connectSmsAPI(String mobile, String smsCode){
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
        HashMap<String, Object> result = null;
        restAPI.init("app.cloopen.com", "8883");
        restAPI.setAccount("8a48b5514b35422d014b36742f0c00b3", "729f9bd1d95e487085e42a18b8bc1ee9");
        restAPI.setAppId("8aaf07085aabcbbd015acfdff3e40b80");
        if(mobile.equals("18649806115")){
            smsCode = "888888";
        }
        result = restAPI.sendTemplateSMS("13705949140","160262" ,new String[]{smsCode,"5"});

        MessageLogs message = new MessageLogs();
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            message = new MessageLogs();
            message.setPHONE(mobile);
            message.setCODE(smsCode);
            message.setCreateTime(new Date().getTime()+"");
            message.setTimeUnit(5);
            message.setSTATUS(result.get("statusCode").toString());
            Map<String, Object> dataMap= (Map<String, Object>) result.get("data");
            message.setSmsMessageSid(((Map<String, Object>)dataMap.get("templateSMS")).get("smsMessageSid").toString());
            message.save();
            return (Map<String, Object>) result.get("data");
        }else{
            //异常返回输出错误码和错误信息
            result.put("errorInfo","错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
        return result;
    }



    public  void base(){

        Record userall= Db.findFirst("select count(*) as num  from user");

        Record useralltoday= Db.findFirst("select count(*) as num from user where to_days(create_time) = to_days(now())");

        Record orderall= Db.findFirst("select count(*) as num from order_list");

        Record orderalltoday= Db.findFirst("select count(*) as num from order_list where to_days(create_time) = to_days(now())");

        Record cash= Db.findFirst("select sum(cash_num) as num from cash_out");

        Record cashtoday= Db.findFirst("select sum(cash_num) as num from cash_out where to_days(create_time) = to_days(now())");

        Record ordercash= Db.findFirst("select sum(total_fee) as num from order_list where status not in (-1,1,6,7)");

        Record ordercashtoday= Db.findFirst("select sum(total_fee) as num from order_list where to_days(create_time) = to_days(now()) and status not in (-1,1,6,7)");


        setAttr("userall",userall.get("num"));

        setAttr("useralltoday",useralltoday.get("num"));

        setAttr("orderall",orderall.get("num"));

        setAttr("orderalltoday",orderalltoday.get("num"));

        float cashsum=0.00f;
        if(cash.get("num") != null){
            cashsum= Integer.valueOf( cash.get("num").toString())/100;
        }

        setAttr("cashsum",cashsum);
        float cashsumtoday=0.00f;
        if(cashtoday.get("num") != null) {
            cashsumtoday=Integer.valueOf(cashtoday.get("num").toString()) / 100;
        }
        setAttr("cashsumtoday",cashsumtoday);

        float ordersum=0.00f;
        if(ordercash.get("num") != null){
            ordersum=      Integer.valueOf( ordercash.get("num").toString())/100;
        }


        setAttr("ordersum",ordersum);

        float ordersumtoday= 0.00f;
        if(ordercashtoday.get("num") != null) {
            ordersumtoday=  Integer.valueOf(ordercashtoday.get("num").toString()) / 100;
        }
        setAttr("ordersumtoday",ordersumtoday);


        renderVelocity("/newshop/fast/fast.html");
    }
}
